﻿using System;
using System.Collections.Generic;
using System.IO;

namespace SuperKit
{
    public class FileGroup {
        public DirectoryInfo Dir { get; }
        public FileInfo[] Files { get; private set; }
        /// <summary>此文件相对于主目录的层次</summary>
        public int Layer { get; }

        public int Count => Files == null ? 0 : Files.Length;
        public bool IsEmpty => Files == null || Files.Length == 0;

        public FileGroup(DirectoryInfo dir, int layer) {
            Dir = dir;
            Files = dir.GetFiles();
            Layer = layer;
        }


        public void Filter(string[] exts) {
            if (IsEmpty) { return; }

            List<FileInfo> list = new List<FileInfo>();
            foreach (var i in Files) {
                if (Kit.IsFileType(i, exts)) {
                    list.Add(i);
                }
            }

            Files = list.ToArray();
        }

        public static List<FileGroup> GetGroups(DirectoryInfo dir) {
            List<FileGroup> list = new List<FileGroup>();
            GetAllFileGroup(dir, list);
            return list;
        }

        public static void GetAllFileGroup(DirectoryInfo dir, List<FileGroup> list, int layer = 0) {
            list.Add(new FileGroup(dir, layer));
            var dirs = dir.GetDirectories();
            if (dirs != null) {
                foreach (var i in dirs) {
                    GetAllFileGroup(i, list, layer + 1);
                }
            }
        }
    }

}
